require "Common/define"
require "Logic/Config"
require "Common/functions"
require "RedDots/RedDotsHeroWearHandler"
require "RedDots/RedDotsHeroRankHandler"
require "RedDots/RedDotsAssistantSkillHandler"
require "RedDots/RedDotsQuickBattleHandler"
require "RedDots/RedDotsHeroSkillHandler"
require "RedDots/RedDotsTacticsHandler"
require "RedDots/RedDotsSummonHandler"
require "RedDots/RedDotsMailHandler"
require "RedDots/RedDotsFriendApplyHandler"
require "RedDots/RedDotsFriendLoveHandler"
require "RedDots/RedDotsHeroWeaponHandler"
require "RedDots/RedDotsTaskProcessHandler"
require "RedDots/RedDotsTaskBoxHandler"
require "RedDots/RedDotsHeroHandbookRewardHandler"
require "RedDots/RedDotsHeroCrystalHandler"
require "RedDots/RedDotsSevenDayProcessHandler"
require "RedDots/RedDotsActivityCentreHandler"
require "RedDots/RedDotsActivitySidebarHandler"
require "RedDots/RedDotsSevenChallengeHandler"
require "RedDots/RedDotsArenaBattleHandler"
require "RedDots/RedDotsCrossArenaBattleHandler"
require "RedDots/RedDotsCrossTeamArenaBattleHandler"
require "RedDots/RedDotsArenaBoxHandler"
require "RedDots/RedDotsFetterOneHeroHandler"
require "RedDots/RedDotsHeroFetterRewardHandler"
require "RedDots/RedDotsCostHandler"
require "RedDots/RedDotsTaskTimeBoxHandler"
require "RedDots/RedDotsFriendGiftHandler"
require "RedDots/RedDotsFirstPayGiftHandler"
require "RedDots/RedDotsExpeditionHandler"
require "RedDots/RedDotsExpeditionSweepHandler"
require "RedDots/RedDotsTrialHandler"
require "RedDots/RedDotsDrawPondHandler"
require "RedDots/RedDotsWeaponSkinHandler"
require "RedDots/RedDotsBagItemHandler"
require "RedDots/RedDotsHeroLinkHandler"
require "RedDots/RedDotsStoryHandler"
require "RedDots/RedDotsTimeBoxHandler"
require "RedDots/RedDotsChatHandler"
require "RedDots/RedDotsPrivateChatHandler"
require "RedDots/RedDotsPetMatingHandler"
require "RedDots/RedDotsTitleHandler"
require "RedDots/RedDotsClimbTowerHandler"
require "RedDots/RedDotsMythicDungeonsHandler"
require "RedDots/RedDotsTaskTimeBoxKeyEnoughHandler"
require "RedDots/RedDotsBillboardHandler"
require "RedDots/RedDotsRankBattleHandler"
require "RedDots/RedDotsHeadFrameHandler"
require "RedDots/RedDotsFriendBattle"
require"RedDots/RedDotsRefineHandler"
require"RedDots/RedDotsGuildApplyHandler"
require"RedDots/RedDotsGuildBattlePlusApplyHandler"
require"RedDots/RedDotsWorldMapHandler" -- 世界地图红点
require"RedDots/RedDotsWorldMapRevengeListHandler" -- 世界地图复仇列表红点
require"RedDots/RedDotsWorldMapRevengeTabListHandler"
require"RedDots/RedDotsWorldMapReceiveRobRewardHandler" -- 世界地图领取奖励红点
require"RedDots/RedDotsWorldMapSearchHandler" -- 世界地图搜索红点
require"RedDots/RedDotsActivateHeroHandler" --碎片激活英雄
require"RedDots/RedDotsBattleRecommendHandler" --阵容推荐
require"RedDots/RedDotsGuildBossHandler"--公会boss
require"RedDots/RedDotsGuildWishHandler"--公会心愿
require"RedDots/RedDotsUnlockDescHandler"--功能解锁
require"RedDots/RedDotsFormationDefense"--防守阵容
require"RedDots/RedDotsDrawWishListHandler"--心愿单
require"RedDots/RedDotsDebrisHandler"--奇异碎片升级
require"RedDots/RedDotsDarkStreetHandler"--奇异碎片升级
require"RedDots/RedDotsVoucherRewardHandler"--返利
require"RedDots/RedDotsVipHandler"--vip可领
require"RedDots/RedDotsSevenRankHandler"--七天排行
require"RedDots/RedDotsCrossTeamRewardHandler"
require"RedDots/RedDotsUIRankingLikeHandler"
require"RedDots/RedDotsRealManHandler"--深渊试炼
require"RedDots/RedDotsSevenRankPlusHandler"--七天排行
require"RedDots/RedDotsFlashGiftHandler"--限时礼包

require"RedDots/RedDotsGuildHelpRobHandler"
require "RedDots/RedDotsInvasionRewardHandler"
require "RedDots/RedDotsInvasionKickedHandler"
require "RedDots/RedDotsInvasionOpenHandler"
require "RedDots/RedDotsEquipBreakHandler"
require "RedDots/RedDotsGuildTechHandler"
require "RedDots/RedDotsOneKeyRefineHandler"
require "RedDots/RedDotsHeroInteractionRewardHandler"
require "RedDots/RedDotsHeroInteractionPointHandler"
require "RedDots/RedDotsCostGiftLinkHandler"
require "RedDots/ReDotsHookReward"
require "RedDots/RedDotsPetPartUpHandler"
require "RedDots/RedDotsHardStoryHandler"
require "RedDots/RedDotsGuildAllyHandler"
require "RedDots/RedDotsGuildTaskHandler"
require "RedDots/RedDotsGuildGiftHandler"
require "RedDots/RedDotsHeroSoulHandler"
require "RedDots/ReDotsInvasionHighRewardHandler"
require "RedDots/RedDotsPotentialHandler"
require "RedDots/ReDotsUISpeedHandler"
require "RedDots/RedDotsRuneLevelUpHandler"
require "RedDots/RedDotsRuneWearHandler"
require "RedDots/RedDotsRuneChangeHandler"
require "RedDots/RedDotsHeroManorHandler"
require "RedDots/RedDotsHeroManorAutoHandler"

RedDotsManager ={};
local this = RedDotsManager;

local tableHandler={}; --处理器集合
local tableEvent={};  --事件注册集合

local tableSubReddots={};  --红点集合下面关联的红点处理器
local tableParentReddots={}; -- 红点处理器相关联的红点集合

local tabCurrentBroadcast={}; --需要通知的红点
local brostTimer =nil; -- 帧尾刷新，防止同一帧计算多次重复红点

function RedDotsManager.init()
    tableEvent = {};
    tableHandler = {};
    tableSubReddots ={};
    tableParentReddots={};

    tabCurrentBroadcast ={};
    if brostTimer~=nil then
        LateUpdateBeat:RemoveListener(brostTimer);
    end
    brostTimer = nil;

    local function _seekSubReddots(type,typeTable,excutedTypes)
        if excutedTypes[type]~=nil then
            return;
        end
        excutedTypes[type] = type;
        if Config.RedDotsCollect[type]~=nil then
            for key, value in pairs(Config.RedDotsCollect[type]) do
                _seekSubReddots(value,typeTable,excutedTypes);
            end
        else
            table.insert(typeTable,type);
        end
    end

    local function _seekParentReddots(type,typeTable,excutedTypes)
        if excutedTypes[type]~=nil then
            return;
        end
        excutedTypes[type] = type;

        for key, value in pairs(Config.RedDotsCollect) do
            for i=1,#value do
                if value[i] == type then
                    table.insert(typeTable,key);
                    _seekParentReddots(key,typeTable,excutedTypes);
                    break;
                end
            end
        end
    end

    for key, value in pairs(Config.RedDotsType) do
        if Config.RedDotsCollect[value]~=nil then
            tableSubReddots[value] = {};
            _seekSubReddots(value,tableSubReddots[value],{});
        else
            tableParentReddots[value]  ={};
            _seekParentReddots(value,tableParentReddots[value],{});
        end
    end
    
end

function RedDotsManager.addHandler()
 --这里添加handler
    tableHandler[Config.RedDotsType.HeroWear] = RedDotsHeroWearHandler.init();
    tableHandler[Config.RedDotsType.HeroRank] = RedDotsHeroRankHandler.init();
    tableHandler[Config.RedDotsType.HeroSkill] = RedDotsHeroSkillHandler.init();
    tableHandler[Config.RedDotsType.Tactics] = RedDotsTacticsHandler.init();
    tableHandler[Config.RedDotsType.Summon] = RedDotsSummonHandler.init();
    tableHandler[Config.RedDotsType.Mail] = RedDotsMailHandler.init();
    tableHandler[Config.RedDotsType.FriendApply] = RedDotsFriendApplyHandler.init();
    tableHandler[Config.RedDotsType.FriendLove] = RedDotsFriendLoveHandler.init();
    tableHandler[Config.RedDotsType.HeroWeapon] = RedDotsHeroWeaponHandler.init();
    tableHandler[Config.RedDotsType.TaskProcess] = RedDotsTaskProcessHandler.init();
    tableHandler[Config.RedDotsType.TaskBox] = RedDotsTaskBoxHandler.init();
    tableHandler[Config.RedDotsType.HeroHandbookReward] = RedDotsHeroHandbookRewardHandler.init();
    tableHandler[Config.RedDotsType.HeroCrystal] = RedDotsHeroCrystalHandler.init();
    tableHandler[Config.RedDotsType.Sevenday] = RedDotsSevenDayProcessHandler.init();
    tableHandler[Config.RedDotsType.SevenChallenge] = RedDotsSevenChallengeHandler.init();
    tableHandler[Config.RedDotsType.ArenaBattle] = RedDotsArenaBattleHandler.init();
    tableHandler[Config.RedDotsType.CrossArenaBattle] = RedDotsCrossArenaBattleHandler.init()
    tableHandler[Config.RedDotsType.CrossTeamArenaBattle] = RedDotsCrossTeamArenaBattleHandler.init()
    tableHandler[Config.RedDotsType.ArenaBox] = RedDotsArenaBoxHandler.init();
    tableHandler[Config.RedDotsType.OneHeroFetter] = RedDotsFetterOneHeroHandler.init();
    tableHandler[Config.RedDotsType.HeroFetterReward] = RedDotsHeroFetterRewardHandler.init();
    tableHandler[Config.RedDotsType.Cost] = RedDotsCostHandler.init();
	tableHandler[Config.RedDotsType.TaskTimeBox] = RedDotsTaskTimeBoxHandler.init();
    tableHandler[Config.RedDotsType.FriendGift] = RedDotsFriendGiftHandler.init();
    tableHandler[Config.RedDotsType.FirstPayGift] = RedDotsFirstPayGiftHandler.init();
    tableHandler[Config.RedDotsType.Expedition] = RedDotsExpeditionHandler.init();
    tableHandler[Config.RedDotsType.ExpeditionSweep] = RedDotsExpeditionSweepHandler.init();
    tableHandler[Config.RedDotsType.Trials] = RedDotsTrialHandler.init();
    tableHandler[Config.RedDotsType.FreeFastBattle] = RedDotsQuickBattleHandler.init();
    tableHandler[Config.RedDotsType.AssistantSkill] = RedDotsAssistantSkillHandler.init();
    tableHandler[Config.RedDotsType.DrawPond] = RedDotsDrawPondHandler.init();
    tableHandler[Config.RedDotsType.Title] = RedDotsTitleHandler.init();
    tableHandler[Config.RedDotsType.WeaponSkin] = RedDotsWeaponSkinHandler.init()
    tableHandler[Config.RedDotsType.BagItem] = RedDotsBagItemHandler.init()
    tableHandler[Config.RedDotsType.HeroLink] = RedDotsHeroLinkHandler.init()
    tableHandler[Config.RedDotsType.TimeBoxAdd] = RedDotsTimeBoxHandler.init();
    tableHandler[Config.RedDotsType.Chat] = RedDotsChatHandler.init();
    tableHandler[Config.RedDotsType.Story] = RedDotsStoryHandler.init();
    tableHandler[Config.RedDotsType.PrivateChat] = RedDotsPrivateChatHandler.init();
    tableHandler[Config.RedDotsType.PetMating] = RedDotsPetMatingHandler.init();
    tableHandler[Config.RedDotsType.ClimbTower] = RedDotsClimbTowerHandler.init();
    tableHandler[Config.RedDotsType.ActivityCentre] = RedDotsActivityCentreHandler.init();
    tableHandler[Config.RedDotsType.ActivitySidebar] = RedDotsActivitySidebarHandler.init();
    tableHandler[Config.RedDotsType.MythicDungeons] = RedDotsMythicDungeonsHandler.init();
    tableHandler[Config.RedDotsType.TaskTimeBoxKeyEnough] = RedDotsTaskTimeBoxKeyEnoughHandler.init()
    tableHandler[Config.RedDotsType.Billboard] = RedDotsBillboardHandler.init()
    tableHandler[Config.RedDotsType.RankBattle] = RedDotsRankBattleHandler.init()
    tableHandler[Config.RedDotsType.HeadFrame] = RedDotsHeadFrameHandler.init()
    tableHandler[Config.RedDotsType.FriendBattle] = RedDotsFriendBattle.init()
    tableHandler[Config.RedDotsType.refine] = RedDotsRefineHandler.init()
    tableHandler[Config.RedDotsType.GuildApply] = RedDotsGuildApplyHandler.init()
    tableHandler[Config.RedDotsType.GuildBoss] = RedDotsGuildBossHandler.init()
    tableHandler[Config.RedDotsType.GuildWish] = RedDotsGuildWishHandler.init()
    tableHandler[Config.RedDotsType.GuildBattlePlusApply] = RedDotsGuildBattlePlusApplyHandler.init()

    tableHandler[Config.RedDotsType.WorldMap] = RedDotsWorldMapHandler.init() -- 世界地图红点
    tableHandler[Config.RedDotsType.WorldMapRevengeList] = RedDotsWorldMapRevengeListHandler.init() -- 世界地图复仇列表红点
    tableHandler[Config.RedDotsType.WorldMapRevengeTabList] = RedDotsWorldMapRevengeTabListHandler.init() 
    tableHandler[Config.RedDotsType.WorldMapReceiveRobReward] = RedDotsWorldMapReceiveRobRewardHandler.init() -- 世界地图领取掠夺奖励红点 
    tableHandler[Config.RedDotsType.WorldMapSearch] = RedDotsWorldMapSearchHandler.init() -- 世界地图搜索红点
    tableHandler[Config.RedDotsType.ActivatePartner] = RedDotsActivateHeroHandler.init() -- 碎片激活英雄
    tableHandler[Config.RedDotsType.UnlockDesc] = RedDotsUnlockDescHandler.init() -- 功能解锁
    tableHandler[Config.RedDotsType.defFormation] = RedDotsFormationDefense.init()--防守阵容红点
    tableHandler[Config.RedDotsType.drawWishList] = RedDotsDrawWishListHandler.init()--心愿单
    tableHandler[Config.RedDotsType.debrisUpgrade] = RedDotsDebrisHandler.init()
    tableHandler[Config.RedDotsType.debrisChange] = RedDotsDebrisChangeHandler.init()--
    tableHandler[Config.RedDotsType.debrisBroken] = RedDotsDebrisBrokenHandler.init() -- 魂石突破
    tableHandler[Config.RedDotsType.DarkStreet] = RedDotsDarkStreetHandler.init()--
	tableHandler[Config.RedDotsType.VoucherReward] = RedDotsVoucherRewardHandler.init()--返利
    tableHandler[Config.RedDotsType.VipWard] = RedDotsVipHandler.init()
    tableHandler[Config.RedDotsType.SevenRank] = RedDotsSevenRankHandler.init()--七天排行

    tableHandler[Config.RedDotsType.CrossTeamRewardPreview] = RedDotsCrossTeamRewardHandler.init()-- 
    tableHandler[Config.RedDotsType.UIRankingLike] = RedDotsUIRankingLikeHandler.init()-- 
    tableHandler[Config.RedDotsType.RealMan] = RedDotsRealManHandler.init()-- 深渊试炼

    tableHandler[Config.RedDotsType.GuildHelpRob] = RedDotsGuildHelpRobHandler.init()
    tableHandler[Config.RedDotsType.InvasionOpen]=RedDotsInvasionOpenHandler.init()
    tableHandler[Config.RedDotsType.InvasionReward]=RedDotsInvasionRewardHandler.init()
    tableHandler[Config.RedDotsType.InvasionKicked]=RedDotsInvasionKickedHandler.init()
    tableHandler[Config.RedDotsType.EquipBreak] = RedDotsEquipBreakHandler.init()
    tableHandler[Config.RedDotsType.GuildTech] = RedDotsGuildTechHandler.init() -- 公会科技
    tableHandler[Config.RedDotsType.OneKeyRefine] = RedDotsOneKeyRefineHandler.init()
    tableHandler[Config.RedDotsType.HeroInteractionReward]=RedDotsHeroInteractionRewardHandler.init()
    tableHandler[Config.RedDotsType.HeroInteractionPoint]=RedDotsHeroInteractionPointHandler.init()
    tableHandler[Config.RedDotsType.BattleRecommend]=RedDotsBattleRecommendHandler.init()
    tableHandler[Config.RedDotsType.CostGiftLink]=RedDotsCostGiftLinkHandler.init()  -- 集市破冰小红点
    tableHandler[Config.RedDotsType.HookReward] = ReDotsHookReward.init()
    tableHandler[Config.RedDotsType.SevenRankPlus] = RedDotsSevenRankPlusHandler.init() --七日排行
    tableHandler[Config.RedDotsType.FlashGift] = RedDotsFlashGiftHandler.init() --限时礼包
    tableHandler[Config.RedDotsType.PetPartUp] = RedDotsPetPartUpHandler.init()--灵饰红点
    tableHandler[Config.RedDotsType.HardStory] = RedDotsHardStoryHandler.init()--剧情模式
    tableHandler[Config.RedDotsType.GuildAlly] = RedDotsGuildAllyHandler.init()--公会结盟
    tableHandler[Config.RedDotsType.GuildTask] = RedDotsGuildTaskHandler.init()--工会任务
    tableHandler[Config.RedDotsType.GuildGift] = RedDotsGuildGiftHandler.init()--工会任务
    tableHandler[Config.RedDotsType.HeroSoul] = RedDotsHeroSoulHandler.init()--将魂
    tableHandler[Config.RedDotsType.InvasionHighReward] = ReDotsInvasionHighRewardHandler.init()
    tableHandler[Config.RedDotsType.Potential] = RedDotsPotentialHandler.init()
    tableHandler[Config.RedDotsType.uiSpeed] = ReDotsUISpeedHandler.init()
    tableHandler[Config.RedDotsType.RuneWear] = RedDotsRuneWearHandler.init()
    tableHandler[Config.RedDotsType.RuneLevelUp] = RedDotsRuneLevelUpHandler.init()
    tableHandler[Config.RedDotsType.RuneChange] = RedDotsRuneChangeHandler.init()
    tableHandler[Config.RedDotsType.HeroManor] = RedDotsHeroManorHandler.init()
    tableHandler[Config.RedDotsType.HeroManorAuto] = RedDotsHeroManorAutoHandler.init()
end

function RedDotsManager.addEventListen(redDotsType,func)
    if tableEvent[redDotsType] ==nil then
        tableEvent[redDotsType] = {};
    end
    table.insert(tableEvent[redDotsType],func);
end

function RedDotsManager.removeEventListen(redDotsType,func)
   if tableEvent[redDotsType] ~=nil then
       table.removeByValue(tableEvent[redDotsType],func);
   end
end

function RedDotsManager.getFlagState(redDotsType,...)
    local state = Config.RedDotsFlagState.None;
    if Config.RedDotsCollect[redDotsType]~=nil then
        if redDotsType == Config.RedDotsType.RootCity and not logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.City) then
            return state;
        end
        if tableSubReddots[redDotsType]==nil then
            logError(string.format("id为%d的红点错误：作为红点集合，没有具体的子红点类型",redDotsType));
        else
            for i = 1, #tableSubReddots[redDotsType] do
                local handler = tableHandler[tableSubReddots[redDotsType][i]];
                if handler~=nil then
                    state = handler.getFlag(...);
                    if state == Config.RedDotsFlagState.Needful then
                        break;
                    end
                end
            end
        end
    else
        local handler = tableHandler[redDotsType];
        if handler~=nil then
            state = handler.getFlag(...);
       end
    end

    return state;
end

function RedDotsManager.addBroadcast(redDotsType)
    local function _broadcast(type)
        for i=1,#tabCurrentBroadcast do
            if tabCurrentBroadcast[i] == type then
                return;
            end
        end
        table.insert(tabCurrentBroadcast,type);
        if brostTimer == nil then
            brostTimer = LateUpdateBeat:Add(this.broadcast,this);
            LateUpdateBeat:AddListener(brostTimer);
        end
    end

    _broadcast(redDotsType);
    if tableParentReddots[redDotsType]~=nil then
        --dump(tableParentReddots[redDotsType])
        for i = 1, #tableParentReddots[redDotsType] do
            _broadcast(tableParentReddots[redDotsType][i]);
        end
    end
end

function RedDotsManager.broadcast()
    for i=1,#tabCurrentBroadcast do
        local tableEventSingle = tableEvent[tabCurrentBroadcast[i]];
        if tableEventSingle~=nil then
            for k = 1, #tableEventSingle do
                tableEventSingle[k]();
            end
        end
    end
    tabCurrentBroadcast = {};
    LateUpdateBeat:RemoveListener(brostTimer);
    brostTimer = nil;
end
function RedDotsManager.Clear()
    tabCurrentBroadcast = {}
    if brostTimer then
        LateUpdateBeat:RemoveListener(brostTimer);
        brostTimer = nil
    end
end

return RedDotsManager